package com.ts.manager;

import com.easemob.EMCallBack;
import com.easemob.chat.CmdMessageBody;
import com.easemob.chat.EMChatManager;
import com.easemob.chat.EMGroupManager;
import com.easemob.chat.EMMessage;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.rio.core.L;
import com.rio.core.U;
import com.rio.helper.json.G;
import com.rio.layout.TaskManager;
import com.rio.layout.view.SimpleTask;
import com.ts.api.*;
import com.ts.data.APIExecuteOrderAction;
import com.ts.data.APIExecuteMachineAction;
import com.ts.host.APP;
import com.ts.host.Event;
import com.ts.host.Event.CloseWater;
import com.ts.host.Event.OpenWater;

import de.greenrobot.event.EventBus;

public class HttpMananger {

	public static void regMachine() {
		RegApi api = new RegApi();
		api.mid = MachineManager.getInstance().getDeviceToken();
		HttpUtils http = new HttpUtils();
		http.send(HttpRequest.HttpMethod.GET, api.getRequest(),
				new RequestCallBack<String>() {
					@Override
					public void onLoading(long total, long current,
							boolean isUploading) {

					}

					@Override
					public void onSuccess(
							ResponseInfo<String> responseInfo) {
						TaskManager.getInstance().async(new SimpleTask() {

							@Override
							public Object onBGThread(Object... params)
									throws Exception {
								if (U.size(1, params)) {
									String responseInfo =U.findById(params, 0);
									final APIExecuteMachineAction machine = G.toObject(
											responseInfo, APIExecuteMachineAction.class);
									if (U.notNull(machine)&& U.notNull(machine.data) && U.notNull(machine.data.HXusername)
											&& U.notNull(machine.data.HXpassword)) {//此方法为异步方法
										EMChatManager.getInstance().login(
												machine.data.HXusername,
												machine.data.HXpassword,
												new EMCallBack() {// 回调
													@Override
													public void onSuccess() {
														MachineManager.getInstance().setMachine(machine.data);
														WaterManager.getInstance().setMode(WaterManager.MODE_CHARGING);
													}

													@Override
													public void onProgress(int progress,String status) {

													}

													@Override
													public void onError(int code,String message) {														
														WaterManager.getInstance().setMode(WaterManager.MODE_FREE);
													}
												});
									}else{
										WaterManager.getInstance().setMode(WaterManager.MODE_FREE);
									}

								}
								return super.onBGThread(params);
							}
							


						},responseInfo.result);
					}

					@Override
					public void onStart() {
					}

					@Override
					public void onFailure(HttpException error, String msg) {
						L.i(msg);
						WaterManager.getInstance().setMode(WaterManager.MODE_FREE);
					}
				});

	}

	public static void order(OpenWater open, CloseWater close) {
		if (U.notNull(open) && U.notNull(close) && !APP.TAG.equals(open.platform)) {
			
			
			AddOrderApi api = new AddOrderApi();
			api.account = U.toString(open.account);
			api.machinecode = MachineManager.getInstance().getMachine().machinecode;
			api.machineID = MachineManager.getInstance().getMachine().machineID;
			api.schoolID = MachineManager.getInstance().getMachine().schoolID;
			api.schoolname = MachineManager.getInstance().getMachine().school;
			api.platform = open.platform;
			api.userID = open.userID;
			api.username  = open.username;
			api.phone = open.phone;
			api.cuid = open.cuid;
			api.realaccount = U.toString(open.account);
			api.money = "0";			
			HttpUtils http = new HttpUtils();
			http.send(HttpRequest.HttpMethod.GET, api.getRequest(),
					new RequestCallBack<String>() {

						@Override
						public void onSuccess(
								ResponseInfo<String> responseInfo) {

						}


						@Override
						public void onFailure(HttpException error, String msg) {
							L.i(msg);
						}
					});

		}

	}
	
	public static void getMachineAddress(){
		if(MachineManager.getInstance().isConnected()){
			
			AddressApi api = new AddressApi();
			api.code = MachineManager.getInstance().getMachine().machinecode;
			HttpUtils http = new HttpUtils();
			http.send(HttpRequest.HttpMethod.GET, api.getRequest(),
					new RequestCallBack<String>() {

						@Override
						public void onSuccess(ResponseInfo<String> responseInfo) {
							// TODO Auto-generated method stub
							if(U.notNull(responseInfo)){
								APIExecuteMachineAction action = G.toObject(responseInfo.result, APIExecuteMachineAction.class);
								if(U.notNull(action) && U.notNull(action.data)){
									MachineManager.getInstance().setMachine(action.data);
								}else{
									EventBus.getDefault().post(new Event.GetAddress());
								}
							}
						}

						@Override
						public void onFailure(HttpException error, String msg) {
							// TODO Auto-generated method stub
							EventBus.getDefault().post(new Event.GetAddress());
						}});
			
			
		}
			
	} 

}
